#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *thread_func(void *arg)
{
    printf("Thread %ld waiting...\n", (long)arg);

    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond, &mutex);
    pthread_mutex_unlock(&mutex);

    printf("Thread %ld done.\n", (long)arg);

    return NULL;
}

int main()
{
    pthread_t threads[5];

    for (long i = 0; i < 5; i++)
    {
        pthread_create(&threads[i], NULL, thread_func, (void *)i);
    }

    sleep(2); // 等待2秒钟

    printf("Signaling...\n");
    for (long i = 0; i < 4; i++)
    {
        pthread_cond_signal(&cond);
    }

    for (long i = 0; i < 5; i++)
    {
        pthread_join(threads[i], NULL);
    }

    return EXIT_SUCCESS;
}